package fy.hr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import fy.config.FyConfig;
import fy.hr.bean.PersonInfoBean;
import fy.util.DBUtil;

public class FindPersonInfoDao {

	private static Log log = LogFactory.getLog(FindPersonInfoDao.class);

	private String[] codes;

	private boolean needTime = false;
	private boolean newAway = false;

	private final static String chuchaiSQL = "select tab1.psncode, tab1.psnname, tab2.deptname,tab3.jobname, p.pk_tbm_psndoc, tab1.pk_corp from bd_psndoc tab1 join bd_deptdoc tab2 on tab1.pk_deptdoc=tab2.pk_deptdoc join om_job tab3 on tab1.pk_om_job=tab3.pk_om_job left join tbm_psndoc p on tab1.pk_psndoc = p.pk_psndoc where  to_date(p.enddate,'yyyy-mm-dd') > sysdate and psnclscope=0 ";

	private final static String yingongSQL = "select tab1.psncode,tab1.psnname,tab2.deptname,tab3.jobname, p.pk_tbm_psndoc, tab1.pk_corp, tab4.basgroupdef4 from bd_psndoc tab1 join bd_deptdoc tab2 on tab1.pk_deptdoc=tab2.pk_deptdoc join om_job tab3 on tab1.pk_om_job=tab3.pk_om_job join bd_psnbasdoc tab4 on tab1.pk_psnbasdoc=tab4.pk_psnbasdoc left join tbm_psndoc p on tab1.pk_psndoc = p.pk_psndoc where to_date(p.enddate,'yyyy-mm-dd') > sysdate and psnclscope=0 ";

	public JSONObject findPersonInfo() {
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		JSONObject jo = new JSONObject();
		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());

			st = conn.prepareStatement(getSQL());
			setParams(st);
			rs = st.executeQuery();

			while (rs.next()) {
				PersonInfoBean pib = new PersonInfoBean();
				pib.setCode(rs.getString(1));
				pib.setName(rs.getString(2));
				pib.setDep(rs.getString(3));
				pib.setPost(rs.getString(4));
				String pk_tbm_psndoc = rs.getString(5);
				pib.setExistPk_tbm_psndoc((pk_tbm_psndoc == null
						|| "".equals(pk_tbm_psndoc) ? false : true));
				pib.setPk_corp(rs.getString(6));
				if (this.needTime) {
					pib.setDate(rs.getString(7));
				}
				if(this.newAway){
				/*	pib.setBasgroupdef23(groupRecord.getFieldValue("basgroupdef23"));
					pib.setId(groupRecord.getFieldValue("id"));
					if(code==0){
					pib.setMobile(groupRecord.getFieldValue("mobile"));}*/
				}

				jo.put(pib.getID(), pib.getJSONObject());
			}

		} catch (Exception e) {
			log.error("查找人员信息出错", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(st);
			DBUtil.close(conn);
		}

		return jo;
	}

	public void setCodes(String[] codes) {
		this.codes = codes;
	}

	private String getSQL() {
		StringBuilder sb = new StringBuilder();

		sb.append(needTime ? yingongSQL : chuchaiSQL);

		for (int i = 0, length = this.codes.length; i < length; i++) {
			if (i == 0) {
				sb.append(" and ( tab1.psncode=? ");
			} else {
				sb.append(" or tab1.psncode=? ");
			}
		}
		sb.append(" ) ");

		return sb.toString();
	}

	private void setParams(PreparedStatement st) throws Exception {
		for (int i = 0, length = this.codes.length; i < length; i++) {
			st.setString(i + 1, this.codes[i]);
		}
	}

	public void setNeedTime(boolean needTime) {
		this.needTime = needTime;
	}
	public void setNewAway(boolean newAway) {
		this.newAway = newAway;
	}

}
